/**   
 * @Title: RechargeMenuPage.java 
 * @Package com.esun.elk.ui.views.userinfo.recharge 
 * @Description: 充值功能选项界面
 * @author Miko baoz@500wan.com   
 * @date 2011-5-25 上午11:50:40  
 * copy right 深圳市易讯天空网络技术有限公司
 */
package com.esun.cheetah.view.personal.recharg;

import android.app.Activity;
//import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.personalcommunicater.CheckCardCommunicater;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.view.custom.CustomConfirmDialog;
import com.esun.cheetah.view.custom.CustomProgressDialog;
import com.esun.cheetah.view.personal.RechargeUninUnbinding;
import com.esun.cheetah.view.setting.connect.ConnectionChangeReceiver;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * 
 * @Package com.esun.cheetah.view.personal.recharg.RechargeMenuPage
 * @ClassName: RechargeMenuPage
 * @Description: 充值功能选项界面
 * @author 孔祥攀 <br/>
 * @date 2012-4-12 上午9:30:14
 * 
 */
public class RechargeMenuPage extends Activity implements OnClickListener {

	// Activity 请求代码
	public static final int REQUEST_CODE_BANKCAD = 1;

	// Activity返回代码
	public static final int RESULT_CODE_TO_CENTER = 10;
	public static final int RESULT_CODE_TO_BUY_LOTTERY = 11;

	private View btnUnin;
	private View btnShenzhouxing;
	// private RelativeLayout btnZhiFuBao;
	// private RelativeLayout btnCft;
	private Button btnGoBack;

//	private Handler handler;
	private CheckCardHandler mCheckCardHandler;

//	private ProgressDialog progress;
//
//	private final int SHENZHOUXING_PREPARE_OK = 10000;
//	private final int UNIN_PREPARE_OK = 10001;
//	private final int NOT_BINDING_UNIN_CARD = 10002;
//	private final int CAN_NOT_PAY_BY_PHONE = 22222;
//
//	private final int PREPARE_FAILED = 111111;

	private CheckCardCommunicater mCheckCardCommunicater;

	private CustomProgressDialog mCustomProgressDialog;

	public static boolean isRechargeSucceed = false;

	// 绑定银行卡类型

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userinfo_detail_recharge);

		initUI();
//		initHandler();
	}

	private void initUI() {
		btnUnin = findViewById(R.id.userinfo_detail_recharge_btn_unin);
		btnUnin.setOnClickListener(this);

		btnShenzhouxing = findViewById(R.id.userinfo_detail_recharge_btn_szx);
		btnShenzhouxing.setOnClickListener(this);

		// btnZhiFuBao = (RelativeLayout)
		// findViewById(R.id.userinfo_detail_recharge_btn_zfb);
		// btnZhiFuBao.setOnClickListener(this);

		// kuroky added 2012-02-07
		// btnCft = (RelativeLayout)
		// findViewById(R.id.userinfo_detail_recharge_btn_cft);
		// btnCft.setOnClickListener(this);

		btnGoBack = (Button) findViewById(R.id.userinfo_detail_recharge_goBack);
		btnGoBack.setOnClickListener(this);

//		progress = ViewerUtil.showProgressDialog(this, "用户信息获取中...");
	}

//	private void initHandler() {
//		handler = new Handler() {

//			@Override
//			public void handleMessage(Message msg) {
//				super.handleMessage(msg);

//				progress.cancel();

//				switch (msg.what) {
//
//				case SHENZHOUXING_PREPARE_OK:

//					startActivity(new Intent().setClass(RechargeMenuPage.this,
//							RechargeShenzhouxin.class));
//
//					break;

//				case UNIN_PREPARE_OK:

					// startActivity(new
					// Intent().setClass(RechargeMenuPage.this,
					// RechargeUninBinding.class));

//					break;

//				case NOT_BINDING_UNIN_CARD:

					// startActivity(new
					// Intent().setClass(RechargeMenuPage.this,
					// RechargeUninUnbinding.class));

//					break;

//				case CAN_NOT_PAY_BY_PHONE:

					// ViewerUtil.showTipDialog(RechargeMenuPage.this,
					// rechargeController.getMSG(), false);

//					break;

//				case PREPARE_FAILED:

//					ViewerUtil.showTipDialog(RechargeMenuPage.this, "获取用户信息失败",
//							false);

//					break;
//				}

//			}
//		};
//	}

	// private boolean checkUserLogin() {
	//
	// if (RechargeUserInfo.getUserInfo().getSessionId().length() > 5) {
	// return true;
	// } else {
	// boolean b = false;
	//
	// try {
	//
	// b = loginRecharge.login(UserInfo.getUserInfo().getUserName(),
	// UserInfo.getUserInfo().getUserPwd());
	// } catch (Exception e) {
	// EDebug.o(this, "Error" + e.getLocalizedMessage());
	// b = false;
	// }
	// return b;
	// }
	// }

	// private void prepareUnin() {
	// new Thread() {
	// @Override
	// public void run() {
	// super.run();
	//
	// // 是否登陆成功
	// if (checkUserLogin()) {
	//
	// // 是否获取用户详情成功
	// if
	// (detailInfoController.getUserInfoDetail(UserInfo.getUserInfo().getUserName(),
	// RechargeUserInfo.getUserInfo().getSessionId()).length > 1) {
	//
	// // 是否绑定银行卡
	// if (RechargeUserInfo.getUserInfo().isBandingCard()) {
	//
	// // 是否可以使用手机银联充值
	// if (rechargeController.isCanPhoneRecharge()) {
	// Message msg = handler.obtainMessage(UNIN_PREPARE_OK);
	// handler.sendMessage(msg);
	// } else {
	// Message msg = handler.obtainMessage(CAN_NOT_PAY_BY_PHONE);
	// handler.sendMessage(msg);
	// }
	//
	// } else {
	// Message msg = handler.obtainMessage(NOT_BINDING_UNIN_CARD);
	// handler.sendMessage(msg);
	// }
	// } else {
	// Message msg = handler.obtainMessage(PREPARE_FAILED);
	// handler.sendMessage(msg);
	// }
	//
	// } else {
	// Message msg = handler.obtainMessage(PREPARE_FAILED);
	// handler.sendMessage(msg);
	// }
	// }
	// }.start();
	// }

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.userinfo_detail_recharge_btn_unin:
			mCustomProgressDialog = new CustomProgressDialog(
					RechargeMenuPage.this);
			mCustomProgressDialog.setMessage("请稍后...");
			mCustomProgressDialog.show();
			if (mCheckCardCommunicater == null) {
				if (mCheckCardHandler == null) {
					mCheckCardHandler = new CheckCardHandler();
				}
				mCheckCardCommunicater = new CheckCardCommunicater(
						mCheckCardHandler);
			}
			mCheckCardCommunicater.startCommunicate();

			break;

		case R.id.userinfo_detail_recharge_btn_szx:

			startActivity(new Intent().setClass(RechargeMenuPage.this,
					RechargeShenzhouxin.class));

			break;

		case R.id.userinfo_detail_recharge_goBack:
			UserInfo.getUserInfo().setBuyingRecharge(false);

			finish();

			break;

		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver
				.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.registerReceiver(
				ConnectionChangeReceiver.getConnectionChangeReceiver(),
				ConnectionChangeReceiver.getIntentFilter());
		if (isRechargeSucceed) {
			RechargeMenuPage.this.finish();
			isRechargeSucceed = false;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// UserInfo.getUserInfo().setBuyingRecharge(false);
			finish();
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_CODE_BANKCAD:
			if (resultCode == RESULT_CODE_TO_CENTER) {
				finish();
			} else if (resultCode == RESULT_CODE_TO_BUY_LOTTERY) {
				Intent intentToSportteryFootball = new Intent();
				intentToSportteryFootball
						.setClass(
								RechargeMenuPage.this,
								com.esun.cheetah.view.personal.sportlotteryfootball.SportteryFootballAgp.class);
				Bundle bundleToSportteryFootbal = new Bundle();
				bundleToSportteryFootbal.putInt("CURRENT_TAB_NUM", 0);
				intentToSportteryFootball.putExtras(bundleToSportteryFootbal);
				EDebug.o(this, "click to shuangseqiu choose");
				startActivity(intentToSportteryFootball);
				finish();
			}
		}
	}

	class CheckCardHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			mCustomProgressDialog.dismiss();
			Log.e("RechargeMenuPage", msg.what + "");
			switch (msg.what) {
			case Config.COMMUNICATER_SUCCESS:
				if (mCheckCardCommunicater != null) {
					if (Config.NUM1.equals(mCheckCardCommunicater.getIsbind())) {
						final Intent intent = new Intent(RechargeMenuPage.this,
								RechargeBankActivity.class);
						startActivityForResult(intent, REQUEST_CODE_BANKCAD);
					} else {
						final CustomConfirmDialog dialog = new CustomConfirmDialog(
								RechargeMenuPage.this);
						dialog.setMessage("您还未绑定银行卡。\n如需绑定银行卡请点确定！");
						dialog.setOnConfirmListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								final Intent intent = new Intent(
										RechargeMenuPage.this,
										RechargeUninUnbinding.class);
								startActivity(intent);
								dialog.dismiss();

							}
						});
						dialog.show();
					}
				}
				break;
			case Config.COMMUNICATER_EXCEPTION:
			case Config.COMMUNICATER_FAIL:
				ViewerUtil.showTipDialog(RechargeMenuPage.this, "操作失败", false);
				break;
			default:
				break;
			}
		}

	}

}
